/*
 * @Date: 2023-02-15 15:55:30
 * @LastEditTime: 2023-09-07 16:32:10
 * @FilePath: /wkw/src/api/index.ts
 * 介绍:
 */
import type { AxiosRequestConfig } from "axios";
import Request, { RequestConfig } from "@@/utils/tools/Request";
import { filterObject } from "@@/utils/tools/object";

const env = import.meta.env;

const defaHttp: InstanceType<typeof Request> = new Request(
  {},
  {
    isSuccess(res: any) {
      return {
        success: res.code === 200,
        message: res.msg,
      };
    },
    showLoading() {},
    showFail() {},
    showSuccess() {},
    hidLoading() {},
    filterReq: filterObject,
  }
);
let baseURL: string;
if (env.VITE_PROXY == "1") {
  baseURL = env.VITE_API_PREFIX; //配置默认请求地址--跨域代理
} else {
  baseURL = env.VITE_BASE_URL + env.VITE_API_PREFIX; //配置默认请求地址--无代理
}
defaHttp.axios.defaults.baseURL = baseURL;
defaHttp.axios.defaults.headers.post["Content-Type"] =
  "application/x-www-form-urlencoded";
defaHttp.axios.interceptors.request.use(<any>((
  config: RequestConfig
): AxiosRequestConfig => {
  /**token */
  return { ...config, headers: getHeaderCfg() };
}));

export function getHeaderCfg(oldHeaders?: AnyObject): AnyObject {
  let headers: AnyObject = { ...oldHeaders };
  return headers;
}

export { defaHttp, baseURL };
